<?php
namespace App\Tools;
use App\Models\Order;
use App\Models\Shop;
use App\Models\OrderYlytaking;
use App\Models\ShopRoomTable;
require_once app_path('Tools/print/lib/YLYOpenApiClient.php');
require_once app_path('Tools/print/lib/YLYTokenClient.php');
//打印机指令地址：http://doc2.10ss.net/336832

class  CouldPrint{

    protected $accessToken;
    protected $api;

    public function __construct() {
        $this->accessToken = '7ac8971e6d02437a9844ab78b8b53cfa';
        $this->api = new \YLYOpenApiClient();
    }
//    public function gettokens(){//获取accessToken
//        $token = new \YLYTokenClient();
//        $grantType = 'client_credentials';  //自有模式(client_credentials) || 开放模式(authorization_code)
//        $scope = 'all';                     //权限
//        $timesTamp = time();                //当前服务器时间戳(10位)
//        return json_decode($token->GetToken($grantType,$scope,$timesTamp),true);
//    }
    //添加打印机
    public function addPrint($machineCode,$msign,$printName=''){
        return json_decode($this->api->addPrint($machineCode, $msign,$this->accessToken,$printName,time()),true);
    }
    //删除打印机
    public function delectPrint($machineCode){
        return $this->api->deletePrint($machineCode,$this->accessToken,time());
    }
    //打印机测试
    public function timeTask(){
        $this->api->printIndex('4004561906',$this->accessToken,'定时任务',time(),time());
    }
    //打印菜单
    public function printer($order_id){
        //return;//关闭打印机
        //获取订单
        try {
            $order = Order::find($order_id);
            if (!$order) {
                return;
            }
            //获取打印机
            $printers = $order->printers;
            if (!$printers) {
                return;
            }
            //获取打印内容
            switch ($order->order_type) {
                case 0://外卖
                    //获取打印商家
                    $shopinfo = $order->ShopInfo;
                    if (!$shopinfo) {
                        return;
                    } else {
                        $spend = $shopinfo->spend;
                    }
                    //获取打印菜式
                    $orderdetails = $order->OrderDetail;
                    if (!$orderdetails) {
                        return;
                    }
                    $address = $order->OrderAddress;
                    if (!$address) {
                        return;
                    }
                    return $this->printerTakeout($order, $printers, $orderdetails, $address, $spend);
                    break;
                case 1://预定
                    $orderdetails = $order->OrderDetail; //菜式
                    $orderreserve = $order->orderReserve;//预定信息
                    $shop = $order->shop;             //商家信息
                    if (!$orderdetails || !$orderreserve || !$shop) {
                        return;
                    }
                    return $this->printerReserve($order, $printers, $orderdetails, $orderreserve, $shop->shop_name);
                    break;
                case 3://扫码
                    //获取打印菜式
                    $orderdetails = $order->OrderDetail;
                    return $this->printerScan($order, $printers, $orderdetails);
                    break;
                default://错误
                    return false;
                    break;
            }
        }catch (\Exception $e){
            return false;
        }
    }

    //外卖打印模板
    protected function printerTakeout($order,$printers,$orderdetails,$address,$spend){
        foreach($printers as $v){
            $content  = '';
            if($v->is_useall==0){//全部打印
                //接单设置
                $content .= '<MC>1,00600,0</MC>';
                //打印联数
                $content .= '<MN>'.$v->priter_number.'</MN>';
                $content .= '<FS><LR>外卖订单(掌里通)</LR></FS>';
                $content .= '<FS><LR>订单编号：'.$order->order_number.'</LR></FS>';
                $content .= str_repeat('-',32);
                //呼叫语音
                $content .= '<audio>老板掌里通来生意了，';
                foreach($orderdetails as $k2=>$v2) {
                    $content .= $v2->dishes_name.$v2->goods_number."份";
                }
                $content .= ',8,3</audio>';
                //打印菜单
                $content .= '<FS><table>';
                $content .= '<tr><td>商品</td><td>数量</td><td>单价</td></tr>';
                foreach($orderdetails as $k2=>$v2){
                    if($v2->is_attr==1){
                        //单价计算
                        $spec_data=array_filter(explode(',',$v2->spec));
                        $spec_data_show = '';
                        $spec_data_price =0;
                        foreach($spec_data as $v3){
                            $spec_data_v3=explode('|',$v3);
                            $spec_data_show .= '/'.$spec_data_v3[0];
                            $spec_data_price += $spec_data_v3[1];
                        }
                        $spec_data_price=$v2->sell_price+($spec_data_price*$v2->goods_number);
                        $content .= '<tr><td>'.$v2->dishes_name.'('.$spec_data_show.')'.'</td><td>X'.$v2->goods_number.'</td><td>'. $spec_data_price.'元</td></tr>';
                    }else{
                        $content .= '<tr><td>'.$v2->dishes_name.'</td><td>X'.$v2->goods_number.'</td><td>'.$v2->sell_price.'元</td></tr>';
                    }

                }
                $content .= '</table></FS>';
                $free_money = $order->order_price-$order->order_amount;
                $content .= '<FS><LR>配送费：'.$spend.'</LR></FS>';
                if( $free_money> 0){
                    $content .= '<FS><LR>优惠价：'.number_format($free_money,2).'</LR></FS>';
                }
                $content .= '<FS><LR>总价：'.$order->order_price.'</LR></FS>';
                if($order->note){
                    $content .= '<FS><LR>备注：'.$order->note.'</LR></FS>';
                }
                $content .= str_repeat('-',32);
                //订单详细
                $content .= '<LR>下单时间：'.$order->updated_at.'</LR>';
                $content .= '<LR>用户电话：'.$address->mobile.'('.$address->name.')</LR>';
                $content .= '<LR>送餐地址：'.$address->address.$address->address_detail.'</LR>';
                //尾部
                $printers = $this->api->printIndex($v->machinecode,$this->accessToken,$content,$order->order_number,time());
                $printers = json_decode($printers,true);
                OrderYlytaking::create([
                    'order_id'=>$order->id,
                    'yly_id'=>$printers['body']['id']
                ]);
                //这波
            }else if($v->is_useall==1){//部分打印
                //接单设置
                $content .= '<MC>4,00600,0</MC>';
                //打印联数
                $content .= '<MN>'.$v->priter_number.'</MN>';
                $content .= '<FS><LR>订单编号：'.$order->order_number.'</LR></FS>';
                //呼叫语音
                $content .= '<audio>大厨，老板喊你做菜了，';
                foreach($orderdetails as $k2=>$v2) {
                    $content .= $v2->dishes_name.$v2->goods_number."份";
                }
                $content .= ',8,3</audio>';
                //打印菜单
                foreach($orderdetails as $k2=>$v2){
                    $content .= str_repeat('-',32);
                    if($v2->is_attr==1){
                        $spec = explode(',',$v2->spec);
                        $spec_node='';
                        foreach($spec as $v3){
                            $spec = explode('|',$v3);
                            $spec_node .= $spec[0].'|';
                        }
                        $content .= '<FS2><LR>'.$v2->dishes_name.'(规格：'.$spec_node.')</LR></FS2>';
                    }else{
                        $content .= '<FS2><LR>'.$v2->dishes_name.'</LR></FS2>';
                    }
                    $content .= '<FS2><LR>.............'.$v2->goods_number.'份</LR></FS2>';
                }
                //尾部
                $this->api->printIndex($v->machinecode,$this->accessToken,$content,$order->order_number,time());
            }else{//错误
                return;
            };

        }
    }
    //预定打印模板
    protected function printerReserve($order,$printers,$orderdetails,$orderreserve,$shopname){
        $desk_room_ids = explode('|',$orderreserve->desk_room);
        $desk_room = ShopRoomTable::find($desk_room_ids);//获取预定的桌房
        if(!$desk_room){
            return;
        }
        foreach($printers as $v){
            $content  = '';
            if($v->is_useall==0 && $order->order_status==2){//全部打印
                //接单设置
                $content .= '<MC>4,00600,0</MC>';
                //打印联数
                $content .= '<MN>'.$v->priter_number.'</MN>';
                //头部
                $content .= '<FS><LR>预定订单(掌里通)</LR></FS>';
                $content .= '<FS><LR>订单编号：'.$order->order_number.'</LR></FS>';
                $content .= str_repeat('-',32);
                //呼叫语音
                $content .= '<audio>老板掌里通来生意了,8,3</audio>';

                //打印菜单
                $content .= '<FS><table>';
                $content .= '<tr><td>商品</td><td>数量</td><td>价格</td></tr>';
                foreach($orderdetails as $k2=>$v2){
                    $content .= '<tr><td>'.$v2->dishes_name.'('.$v2->spec.')'.'</td><td>X'.$v2->goods_number.'</td><td>'.$v2->sell_price.'元</td></tr>';
                }
                $content .= '</table></FS>';

                $content .= '<FS><right>订单总价：'.$order->order_price.'</right></FS>';
                $content .= '<FS><right>已付定金：'.$order->order_amount.'</right></FS>';
                $content .= str_repeat('-',32);
                //订单详细
                $desk_room_string = '';
                //打印房桌
                foreach($desk_room as $k2=>$v2){
                    if($v2->type == 0){
                        $desk_room_string.= '桌子：-'.$v2->name.'|';
                    }else if($v2->type == 1){
                        $desk_room_string.= '房间：'.$v2->name.'|';
                    }else if($v2->type == 2){
                        $desk_room_string.= '卡座：'.$v2->name.'|';
                    }
                }
                $content .= '<FS><LR>已定：'.$desk_room_string.'</LR></FS>';
                $content .= '<FS><LR>到店时间：'.$orderreserve->arrive_time.'</LR></FS>';
                $content .= '<FS><LR>贵宾电话：'.$orderreserve->phone.'('.$orderreserve->name.')</LR></FS>';
                $content .= str_repeat('-',32);
                $content .= '<LR>下单时间：'.$order->updated_at.'</LR>';
                //尾部
                $this->api->printIndex($v->machinecode,$this->accessToken,$content,$order->order_number,time());
            }else if($v->is_useall==0 && $order->order_status==4){//结账打印回执
                //接单设置
                $content .= '<MC>4,00600,0</MC>';
                //打印联数
                $content .= '<MN>'.$v->priter_number.'</MN>';
                //头部
                $content .= '<FS><LR>预定订单结账回执</LR></FS>';
                $content .= '<FS><LR>订单编号：'.$order->order_number.'</LR></FS>';
                $content .= str_repeat('-',32);
                //呼叫语音
                $content .= '<audio>你有已结账的订单,8,4</audio>';
                //打印菜单
                $content .= '<FS><table>';
                $content .= '<tr><td>商品</td><td>数量</td><td>价格</td></tr>';
                foreach($orderdetails as $k2=>$v2){
                    $content .= '<tr><td>'.$v2->dishes_name.'('.$v2->spec.')'.'</td><td>X'.$v2->goods_number.'</td><td>'.$v2->sell_price.'元</td></tr>';
                }
                $content .= '</table></FS>';
                $content .= str_repeat('-',32);
                $content .= '<FS><LR>定金：'.$order->order_amount.'元</LR></FS>';
                $content .= '<FS><LR>尾数：'.number_format(($order->order_price-$order->order_amount),2).'元</LR></FS>';
                $content .= '<FS><LR>总价：'.$order->order_price.'元</LR></FS>';
                $content .= str_repeat('-',32);
                //结账时间
                $content .= '<LR>结账时间：'.$order->updated_at.'</LR>';
                //尾部
                $this->api->printIndex($v->machinecode,$this->accessToken,$content,$order->order_number,time());
            }else{
                return;
            };
        }
    }


    //扫码打印模板
    protected function printerScan($order,$printers,$orderdetails){
        foreach($printers as $v){
            $content  = '';
            if($v->is_useall==0){//全部打印
                //接单设置
                $content .= '<MC>4,00120,0</MC>';
                //打印联数
                $content .= '<MN>'.$v->priter_number.'</MN>';
                if($order->order_status==1){
                    $content .= '<FS2><center>(未付款)</center></FS2>';
                }else{
                    $content .= '<FS2><center>(已付款)</center></FS2>';
                }
                $content .= '<FS><LR>扫码点餐(掌里通)</LR></FS>';
                $content .= '<FS><LR>订单编号：'.$order->order_number.'</LR></FS>';
                $content .= str_repeat('-',32);
                //呼叫语音
                $content .= '<audio>老板掌里通来生意了，';
                foreach($orderdetails as $k2=>$v2) {
                    $content .= $v2->dishes_name.$v2->goods_number."份";
                }
                $content .= ',8,3</audio>';
                //打印菜单
                $content .= '<FS><table>';
                $content .= '<tr><td>商品</td><td>数量</td><td>价格</td></tr>';
                foreach($orderdetails as $k2=>$v2){
                    if($v2->is_attr==1){
                        //单价计算
                        $spec_data=array_filter(explode(',',$v2->spec));
                        $spec_data_show = '';
                        $spec_data_price =0;
                        foreach($spec_data as $v3){
                            $spec_data_v3=explode('|',$v3);
                            $spec_data_show .= '/'.$spec_data_v3[0];
                            $spec_data_price += $spec_data_v3[1];
                        }
                        $spec_data_price=$v2->sell_price+($spec_data_price*$v2->goods_number);
                        $content .= '<tr><td>'.$v2->dishes_name.'('.$spec_data_show.')'.'</td><td>X'.$v2->goods_number.'</td><td>'.$spec_data_price.'元</td></tr>';
                        //$content .= '<tr><td>'.$v2->dishes_name.'('.$v2->spec.')'.'</td><td>X'.$v2->goods_number.'</td><td>'.$v2->sell_price.'元</td></tr>';
                    }else{
                        $content .= '<tr><td>'.$v2->dishes_name.'</td><td>X'.$v2->goods_number.'</td><td>'.$v2->sell_price.'元</td></tr>';
                    }
                }
                $content .= '</table></FS>';
                $content .= '<FS><right>订单总价：'.$order->order_price.'</right></FS>';
                $cheapen = $order->price-$order->order_amount;
                if($cheapen>0){
                    $content .= '<FS><right>单单立减：'.$cheapen.'</right></FS>';
                }
                if($order->note){
                    $content .= '<FS><right>备注：'.$order->note.'</right></FS>';
                }
                $content .= str_repeat('-',32);
                //订单详细
                $content .= '<LR>下单时间：'.$order->updated_at.'</LR>';
                //尾部
                $this->api->printIndex($v->machinecode,$this->accessToken,$content,$order->order_number,time());
            }else if($v->is_useall==1){//部分打印
                //接单设置
                $content .= '<MC>4,00600,0</MC>';
                //打印联数
                $content .= '<MN>'.$v->priter_number.'</MN>';
                $content .= '<FS><LR>订单编号：'.$order->order_number.'</LR></FS>';
                //呼叫语音
                $content .= '<audio>大厨，老板喊你做菜了，';
                foreach($orderdetails as $k2=>$v2) {
                    $content .= $v2->dishes_name.$v2->goods_number."份";
                }
                $content .= ',8,3</audio>';
                //打印菜单
                foreach($orderdetails as $k2=>$v2){
                    $content .= str_repeat('-',32);
                    if($v2->is_attr==1){
                        $spec = explode(',',$v2->spec);
                        $spec_node='';
                        foreach($spec as $v3){
                            $spec = explode('|',$v3);
                            $spec_node .= $spec[0].'|';
                        }
                        $content .= '<FS2><LR>'.$v2->dishes_name.'(规格：'.$spec_node.')</LR></FS2>';
                    }else{
                        $content .= '<FS2><LR>'.$v2->dishes_name.'</LR></FS2>';
                    }
                    $content .= '<FS2><LR>.............'.$v2->goods_number.'份</LR></FS2>';
                }
                //尾部
                $this->api->printIndex($v->machinecode,$this->accessToken,$content,$order->order_number,time());
            }else{//错误
                return;
            };
        }
    }


}
